﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Quartz.Net.Model;
using Quartz.Net.Factory;

namespace Quartz.Net.Core
{
    [DisallowConcurrentExecution]//这次未执行完成，下次不准进来
    public class JobLx : IJob
    {
        public void Execute(IJobExecutionContext context)
        {
            
            List<Task_Quartz_JobTriggerModel> listTask = factoryClass.JobTriggerGetInfo().Where(m => m.VALID == "1").ToList();
            //List<Task_Quartz_JobTriggerModel> oldTask = QuartzHelper.CurrentTaskList.ToList();
            foreach (Task_Quartz_JobTriggerModel taskUtil in listTask)
            {
                if (taskUtil.ISCHANGE == "0")
                {
                    continue;
                }
                else if (taskUtil.ISCHANGE == "1")
                {
                    //QuartzHelper.ScheduleJob(taskUtil,true);
                }
                else if (taskUtil.ISCHANGE == "2")
                {
                    if (taskUtil.RUNSTATUS == "3")
                    {
                        //QuartzHelper.PauseTrigger(taskUtil.TRIGGERNAME, taskUtil.TRIGGERGROUPNAME);
                        //QuartzHelper.PauseJob(taskUtil.JOBNAME, taskUtil.JOBGROUPNAME);
                        QuartzHelper.DeleteJobZt(taskUtil.JOBNAME, taskUtil.JOBGROUPNAME);
                    }
                    else if (taskUtil.RUNSTATUS == "2")
                    {
                        //QuartzHelper.ResumeTrigger(taskUtil.TRIGGERNAME, taskUtil.TRIGGERGROUPNAME);
                        //QuartzHelper.ResumeJob(taskUtil.JOBNAME,taskUtil.JOBGROUPNAME);
                        QuartzHelper.ScheduleJob(taskUtil, false, 1);
                    }
                }
                else if (taskUtil.ISCHANGE == "3")
                {
                    QuartzHelper.ScheduleJob(taskUtil,false,1);
                }
                else if (taskUtil.ISCHANGE == "4")
                {
                    Task_Quartz_JobTriggerModel model = new Task_Quartz_JobTriggerModel();
                    model.NGUID = taskUtil.NGUID;
                    model.JOBNAME = "JobLj1";
                    model.JOBGROUPNAME = "GroupLj1";
                    model.TRIGGERNAME = "Tr1";
                    model.TRIGGERGROUPNAME = "GroupLj1";
                    model.JOBPATH = taskUtil.JOBPATH;
                    model.JOBSNAME = taskUtil.JOBSNAME;
                    model.VALID = "1";
                    model.RUNSTATUS = "1";
                    model.ISCHANGE = "0";

                    QuartzHelper.ScheduleJobLj(model);
                }
                factoryClass.UpdateISChange(taskUtil.NGUID, "0");
            }

        }
    }
}
